
//
//  MeViewController.swift
//  Jock
//
//  Created by HD on 15/4/5.
//  Copyright (c) 2015年 Haidy. All rights reserved.
//

import UIKit

class MeViewController: UIViewController {

    private var userInfoButton: UserInfoButton!
    
    private var config: AppConfig!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        config = AppConfig()
        
        userInfoButton = ViewUtil.instanceView("UserInfoButton") as! UserInfoButton
        userInfoButton.frame = CGRectMake(0, 100, self.view.frame.width, 72)
        self.view.addSubview(userInfoButton)
        userInfoButton.click { (button) -> Void in
            if self.config.isLogin() {
                
            } else {
                self.navigationController?.pushViewController(LoginViewController(), animated: true)
            }
        }
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        showUserInfo()
    }

    //显示用户信息
    private func showUserInfo() {
        if config.isLogin() {
            userInfoButton.nickNameView.text = config.getLogin()
            let userIcon = config.getIcon()
            let userId =  config.getUid()
            let prefixUserId = userId.subStringToIndex(4)
            let url = "http://pic.qiushibaike.com/system/avtnew/\(prefixUserId)/\(userId)/thumb/\(userIcon)"
            ImageLoader.loadImage(url, imageCacheDir: "iconCacheHD", placeHolderName: "avatar.jpg", loadImageView: userInfoButton.headView)
        } else {
            userInfoButton.headView.image = UIImage(named: "default_icon.png")
            userInfoButton.nickNameView.text = "未登录"
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
